import user from '../../../../utils/user'
import {
    getFlowWorkerList,
    exportWorkerPriceList,
    exportadvcnceWorkerPriceList
} from '../../../api/pay'
const app = getApp()
Page({
    data: {
        id: '',
        data: {},
        copyLinkStatus: false,
        copyLinkContent: '',
        pageNum: 1,
        notData: false,
        type: 0
    },
    onLoad: function (options) {
        console.log(options)
        user.waitLogin().then(() => {
            this.setData({
                id: options.id,
                type: options.type * 1
            })
            this.getFlowWorkerList()
        })

    },
    onScroll() {
        user.waitLogin().then(() => {
            this.setData({
                pageNum: this.data.pageNum + 1
            })
            this.getFlowWorkerList()
        })
    },
    getFlowWorkerList() {
        if (this.data.notData) {
            return
        }
        getFlowWorkerList({
            id: this.data.id || null,
            pageSize: 100,
            pageNum: this.data.pageNum
        }).then(res => {
            if (res.personnelList) {
                res.personnelList.forEach((ele, index) => {
                    ele.idCardNo = ele.idCardNo.slice(-4)
                })
            }
            // 时间格式化
            for (const i in res.personnelList) {
                if (res.personnelList[i].signIn) {
                    res.personnelList[i].signIn = res.personnelList[i].signIn.split(' ')[1]
                }
                if (res.personnelList[i].signOut) {
                    res.personnelList[i].signOut = res.personnelList[i].signOut.split(' ')[1]
                }
            }
            if (!res.hasNextPage) {
                this.setData({
                    notData: true
                })
            }
            if (this.data.pageNum === 1) {
                this.setData({
                    data: res
                })
            } else {
                this.data.data.personnelList.push(...res.personnelList)
                this.setData({
                    ['data.personnelList']: this.data.data.personnelList
                })
            }


        })
    },
    onExportList() {
        console.log(this.data.type, 'gyfdsghjdfvhuydfghudfg')
        if (this.data.type === 200) {
            exportadvcnceWorkerPriceList({
                processId: this.data.data.id
            }).then(res => {
                // encodeURIComponent
                const url = `${app.globalData.baseUrl}/common/download?fileName=${encodeURIComponent(res)}&delete=false`
                if (wx.canIUse('openDocument')) {
                    const file = wx.env.USER_DATA_PATH + '/' + this.data.data.projectName.replace(/\//g, '.') + '.xlsx'
                    wx.downloadFile({
                        url,
                        filePath: file,
                        success: function () {
                            const documentPath = file
                            wx.openDocument({
                                showMenu: true,
                                filePath: documentPath,
                                fail: (rej) => {
                                    console.log(rej, 'rej')
                                    this.setData({
                                        copyLinkStatus: true,
                                        copyLinkContent: url
                                    })
                                }
                            })
                        }
                    })
                } else {
                    this.setData({
                        copyLinkStatus: true,
                        copyLinkContent: url
                    })
                }
            })
        } else {
            exportWorkerPriceList({
                processId: this.data.data.id
            }).then(res => {
                // encodeURIComponent
                const url = `${app.globalData.baseUrl}/common/download?fileName=${encodeURIComponent(res)}&delete=false`
                if (wx.canIUse('openDocument')) {
                    const file = wx.env.USER_DATA_PATH + '/' + this.data.data.projectName.replace(/\//g, '.') + '.xlsx'
                    wx.downloadFile({
                        url,
                        filePath: file,
                        success: function () {
                            const documentPath = file
                            wx.openDocument({
                                showMenu: true,
                                filePath: documentPath,
                                fail: (rej) => {
                                    console.log(rej, 'rej')
                                    this.setData({
                                        copyLinkStatus: true,
                                        copyLinkContent: url
                                    })
                                }
                            })
                        }
                    })
                } else {
                    this.setData({
                        copyLinkStatus: true,
                        copyLinkContent: url
                    })
                }
            })
        }

    },
    // 复制名单下载链接
    copyWorkerListLink() {
        const t = this
        wx.setClipboardData({
            data: this.data.data.serveCompanyName + '\r\n' + this.data.copyLinkContent,
            success() {
                t.setData({
                    copyLinkStatus: false
                })
            }
        })
    },
})